package com.example.swaggerDemo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.SysUserEntity;
import com.example.swaggerDemo.service.ArticleService;
import com.example.swaggerDemo.service.UserService;
import com.example.swaggerDemo.util.MyHttpUtils;

import com.example.swaggerDemo.wxpush.xstream;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.nio.charset.Charset;

import java.sql.SQLException;

import java.util.*;


@Api("微信图文推送")
@RestController
@RequestMapping("/articlewx")
public class ArticlewxController {
    @Autowired
    private ArticleService Service;

    @Autowired
    private UserService SysUserDao;

    /**
     * 获取token
     * @param appId
     * @param appSecret
     * @return
     */
    public static String getAccessToken(String appId, String appSecret) {
        StringBuffer userInfoUrl =
                new StringBuffer("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
                        .append("&appid=").append(appId)
                        .append("&secret=").append(appSecret);
        String userInfoStr = MyHttpUtils.doPost(userInfoUrl.toString(), "", Charset.forName("UTF-8").toString());
        Map<String, String> map = null;

        return userInfoStr;
    }

    @ApiOperation(value="",notes="")
    @ResponseBody
    @RequestMapping(value="/findwx",method= RequestMethod.POST)
    public  void SendQYMessageimg(@RequestParam("id") String id)
    {
          String Url="http://qb.kmhkj.top/view/addetails.html?id="+id;
          String mp = getAccessToken("wxd4aff866a1642b28","3baceeec4066458afe6850bb689ec9c6");
          JSONObject rq = JSON.parseObject(mp);
         String accessToken=  rq.get("access_token").toString();
         String strurl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
          List<SysUserEntity> dto  = null;
          try {
              ArticleEntity art = Service.findid(id);
              dto = SysUserDao.findall();
;
              String Title=art.getTitle();
              String Description=art.getPretext();
              String PicUrl="http://qb.kmhkj.top:8080"+ art.getPreimage();
          for (SysUserEntity q: dto)
            {
                String responeJsonStr = "{";
                responeJsonStr += "\"touser\": \"" + q.getOpenid() + "\",";
                responeJsonStr += "\"msgtype\": \"news\",";
                responeJsonStr += "\"agentid\": \"wxd4aff866a1642b28\",";
                responeJsonStr += "\"news\": {";
                responeJsonStr += "\"articles\": [{";
                responeJsonStr += "  \"title\": \"" + Title + "\",";
                responeJsonStr += "  \"description\": \" " + Description + "\",";
                responeJsonStr += "  \"url\": \"" + Url + "\",";
                responeJsonStr += "  \"picurl\": \"" + PicUrl + "\"";
                responeJsonStr += "}]";
                responeJsonStr += "},";
                responeJsonStr += "\"safe\":\"0\"";
                responeJsonStr += "}";
                xstream.connectWeiXinInterface(strurl,responeJsonStr);
            }
          } catch (SQLException e) {
              e.printStackTrace();
          }

    }

}

